import { configureStore } from "@reduxjs/toolkit";
import userReducer from "./modules/user";
import { useDispatch } from "react-redux";
//实现数据持久化
import {
    persistStore,
    persistReducer,
    FLUSH,
    REHYDRATE,
    PAUSE,
    PERSIST,
    PURGE,
    REGISTER,
  } from 'redux-persist'
  import storage from 'redux-persist/lib/storage'
  const persistConfig = {
    key: 'root',
    version: 1,
    storage,
  }



const store = configureStore({
  reducer: {
    user:persistReducer(persistConfig, userReducer), //使用redux-persist进行数据持久化
  },
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: {
        ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
      },
    }),
});

//持久化存储
persistStore(store)


//定义state 类型
export type RootState = ReturnType<typeof store.getState>;
// 定义 dispatch 类型实现异步ts
export type AppDispatch = typeof store.dispatch
export const useAppDispatch = useDispatch.withTypes<AppDispatch>()

export default store;